<template>
	<view class="content" :style="{ paddingBottom: from !== 'personal' && activity.activityForm == '00' ? '110rpx' : '0' }">
		<popup-active :visible.sync="showHonor" :popScreenList="popScreenList" @toPage="toPage"></popup-active>
		<view class="activeSign">
			<view class="title">{{ activity.activityName }}</view>
			<view class="active-changci">
				<view class="massClass" v-if="from !== 'personal' && activity.activityForm == '00'">
					活动场次：
					<text style="color: rgba(102, 100, 92, 0.5);">{{ activity.activityFrequency }}</text>
					<text class="tip">(报名活动请先选择场次)</text>
				</view>

				<radio-group @change="radioChange" class="active-list-item">
					<label class="uni-list-cell uni-list-cell-pd checkList" v-for="(item, index) in timeAddressList" :key="index">
						<view class="check" v-if="showCheckbox(item)">
							<radio
								:value="String(index)"
								:class="{ hidden: item.signUpTotalQuantity && item.totalQuantity && item.signUpTotalQuantity === item.totalQuantity }"
								:checked="index === current"
								color="#FACC6B"
								style="transform: scale(0.8)"
							/>
						</view>
						<view>
							<view class="list-item-time">
								<image class="imageClass" src="https://mall-rongchuang.oss-cn-beijing.aliyuncs.com/guixin_miniProgram/img/calendar.png"></image>
								<text class="time">{{ item.activityTime }}</text>
							</view>
							<view class="list-item-time" v-if="from !== 'personal' && activity.activityForm == '00'">
								<image class="imageClass" src="https://mall-rongchuang.oss-cn-beijing.aliyuncs.com/guixin_miniProgram/img/time.png"></image>
								<view class="time-label">报名时间：</view>
								<view class="time" style="margin-left: 0;">
									<view>{{ item.signUpTime | formatStartDate }}</view>
									<view>{{ item.signUpTime | formatEndDate }}</view>
								</view>
							</view>
							<view class="list-item-time">
								<image
									class="imageClass"
									src="https://mall-rongchuang.oss-cn-beijing.aliyuncs.com/guixin_miniProgram/img/address.png"
									style="width: 26rpx;"
								></image>
								<text class="time">{{ item.activityAddress }}</text>
							</view>
						</view>
					</label>
				</radio-group>
			</view>
		</view>
		<view style="margin:40rpx;"><image :src="activity.activityPic" style="border-radius: 12rpx;width:100%; height: 250rpx;" mode="aspectFill"></image></view>

		<view style="margin: 0 40rpx;padding-bottom: 60rpx;"><rich-text :nodes="activity.activityInfo"></rich-text></view>

		<view v-if="btn"><button @click="detail" style="width: 260rpx;background: #facc6b;height: 82rpx;font-size: 34rpx;">查看详情</button></view>

		<view class="infoTop" v-if="from !== 'personal' && activity.activityForm == '00'">
			<view class="infoButton">
				<!-- <button class="infoButtonClick" @click="comment(activity.activityId)">点评</button> -->
				<button :disabled="signUpDisabled" class="infoButtonClick" @click="forbid(activity.activityId)">报名</button>
			</view>
		</view>

		<uni-popup ref="popup" type="center" class="wrap" v-if="from !== 'personal'">
			<view class="chat">
				<view class="input">
					<view class="item">
						<view class="label">
							<text>姓</text>
							<text>名</text>
						</view>
						<input class="uni-input" name="input" placeholder-style="color: #AEADAA" v-model="name" placeholder="请输入姓名" maxlength="20" />
					</view>
					<view class="item">
						<view class="label">
							<text>手</text>
							<text>机</text>
							<text>号</text>
						</view>
						<input class="uni-input" name="input" placeholder-style="color: #AEADAA" v-model="phone" maxlength="11" placeholder="请输入手机号" type="number" />
					</view>
				</view>
				<button class="btn baseBtn" @click.stop="add()" style="background: #fff;color: rgba(102, 100, 93, 1);font-size: 30rpx;">提交</button>
			</view>
		</uni-popup>
	</view>
</template>

<script>
import http from '../../common/request.js';
import carousel from '@/components/uni-carousel/uni-carousel-task';
import uniPopup from '@/components/uni-popup/uni-popup-reporat.vue';
import popupActive from '@/components/popup-activity/popup-activity.vue';
import { getCommonShareInfo } from '@/common/utils.js';
export default {
	onLoad(option) {
		this.id = option.activityId;
		this.from = option.from;
	},
	components: {
		carousel,
		uniPopup,
		popupActive
	},
	mounted() {
		this.activeList();
	},
	onShareAppMessage() {
		return getCommonShareInfo();
	},
	data() {
		return {
			id: '',
			from: '',
			activity: {},
			current: '',
			timeAddressList: [],
			disabled: false,
			name: '',
			phone: undefined,
			sessionId: '',
			activityId: '',
			popScreenList: [],
			showHonor: false,
			signUpDisabled: false,
			btn: false,
			url: ''
		};
	},
	filters: {
		formatStartDate(date) {
			return date ? date.split('至')[0] + '至' : '';
		},
		formatEndDate(date) {
			return date ? date.split('至')[1] : '';
		}
	},
	computed: {
		showCheckbox() {
			return item => {
				const { from, activity } = this;
				const status = from !== 'personal' && activity.activityForm == '00';
				if (status === false) {
					return status;
				}
				const signUpTime = (item.signUpTime || '').replace(/-/g, '/').replace(/\s+$/, '');
				const splits = signUpTime.split('至');
				const now = Date.now();
				if (new Date(splits[0] + ':00') <= now && new Date(splits[1] + ':00') >= now) {
					return true;
				} else {
					return false;
				}
				return true;
			};
		}
	},
	methods: {
		doIt(list) {
			const result = [];
			for (let item of list) {
				result.push(
					new Promise(resolve => {
						const signUpTime = (item.signUpTime || '').replace(/-/g, '/').replace(/\s+$/, '');
						const splits = signUpTime.split('至');
						const now = Date.now();
						if (new Date(splits[0] + ':00') <= now && new Date(splits[1] + ':00') >= now) {
							resolve(true);//可以报名
						} else {
							resolve(false);//不可以报名
						}
					})
				);
			}
			let flag = true;
			Promise.all(result).then(res => {
				for (let item of res) {
					flag = flag && item;
				}
			})
			return flag;
		},
		toPage(value) {
			http('/api/duiba/getNoLoginUrl?credits=0&redirect=' + value, { method: 'GET' }).then(result => {
				if (result.code == 200) {
					// #ifdef H5
					location.href = result.data;
					// #endif
					// #ifdef MP-WEIXIN
					uni.navigateTo({
						url: '../../home/web_view?url=' + encodeURIComponent(result.data),
						animationType: 'pop-in',
						animationDuration: 200
					});
					// #endif
				}
			});
		},
		activeList() {
			http('/api/activity/appActivityDetail', {
				method: 'GET',
				data: {
					activityId: this.id
				}
			}).then(result => {
				if (result.data === null) {
					uni.showModal({
						title: '',
						content: result.msg,
						showCancel: false,
						confirmColor: '#66645D'
					});
				}
				this.activity = result.data || {};
				let richtext = this.activity.activityInfo || '';
				let regex = new RegExp('<img', 'gi');
				richtext = richtext.replace(regex, `<img style="width: 100%!important;height: auto!important;"`);
				this.activity.activityInfo = richtext;

				this.timeAddressList = this.activity.timeAddressList || [];
				this.disabled = !this.activity.activityStatus === '02';
				if (result.data.contentIsLink != '00') {
					this.btn = true;
					this.url = result.data.activityInfo;
				}

				if (this.activity.activityStatus == '03' || this.activity.activityStatus == '05') {
					this.signUpDisabled = true;
				}
				if (this.activity.activityStatus == '05') {
					uni.showModal({
						title: '',
						content: '您已报名该活动！',
						showCancel: false,
						confirmColor: '#66645D'
					});
					return;
				}
				if(!this.doIt(this.timeAddressList)) {
					this.signUpDisabled = true;
					uni.showModal({
						title: '',
						content: '活动报名已结束！',
						showCancel: false,
						confirmColor: '#66645D'
					});
					return;
				}
				let status = true;
				for (let item of this.timeAddressList) {
					status = status && this.getIfFull(item);
				}
				if (this.signUpDisabled === false && status) {
					this.signUpDisabled = status;
					uni.showModal({
						title: '',
						content: '报名人数已满！',
						showCancel: false,
						confirmColor: '#66645D'
					});
				}
			});
		},
		detail() {
			// #ifdef MP-WEIXIN
			uni.navigateTo({
				url: '../../home/web_view?url=' + encodeURIComponent(),
				animationType: 'pop-in',
				animationDuration: 200
			});
			// #endif
			// #ifdef H5
			location.href = this.url;
			// #endif
		},
		getIfFull(item) {
			return item.signUpTotalQuantity && item.totalQuantity && item.signUpTotalQuantity === item.totalQuantity;
		},
		radioChange(evt) {
			const index = evt.detail.value;
			const activity = this.timeAddressList[index];
			if (this.getIfFull(activity)) {
				return;
			}
			const signUpTime = (activity.signUpTime || '').replace(/-/g, '/').replace(/\s+$/, '');
			const splits = signUpTime.split('至');
			const now = Date.now();
			if (new Date(splits[0] + ':00') <= now && new Date(splits[1] + ':00') >= now) {
				this.sessionId = activity.sessionId;
				this.signUpDisabled = false;
			} else {
				this.signUpDisabled = true;
				if (new Date(splits[0] + ':00') > now) {
					uni.showModal({
						title: '',
						content: '活动未开始报名！',
						showCancel: false,
						confirmColor: '#66645D'
					});
				} else {
					uni.showModal({
						title: '',
						content: '活动已结束报名！',
						showCancel: false,
						confirmColor: '#66645D'
					});
				}
			}
		},
		forbid(data) {
			if (!this.sessionId) {
				uni.showToast({
					title: '请先选择报名场次',
					icon: 'none'
				});
				return;
			}
			this.activityId = data;
			this.$refs.popup.open();
		},
		comment(data) {
			uni.navigateTo({
				url: '/activity/info/comment',
				animationType: 'pop-in',
				animationDuration: 200,
				events: {
					queryDetail: flag => {
						this.detail(flag);
					}
				}
			});
		},
		add() {
			if (!this.phone) {
				uni.showToast({
					title: '请填写手机号码',
					icon: 'none'
				});
				return;
			}
			if (!/^1[0-9]{10,10}$/.test(this.phone)) {
				uni.showToast({
					title: '请输入正确的手机号！',
					icon: 'none'
				});
				return;
			}
			if (!this.name) {
				uni.showToast({
					title: '请填写用户名',
					icon: 'none'
				});
				return;
			}

			http('/api/activity/signUp', {
				data: {
					sessionId: this.sessionId, // 场次id
					activityId: this.activityId, // 活动id
					name: this.name,
					mobile: this.phone
				},
				method: 'POST'
			}).then(
				result => {
					this.$refs.popup.close();
					if (result.code == 200) {
						uni.showToast({
							title: '活动报名成功',
							icon: 'none'
						});
						setTimeout(() => {
							const list = result.popScreen || [];
							if (list.length > 0) {
								this.showHonor = true;
								this.popScreenList = list;
							}
						}, 1000);
					}
				},
				result => {
					this.$refs.popup.close();
				}
			);
		}
	}
};
</script>

<style scoped lang="less">
/deep/radio .wx-radio-input.wx-radio-input-checked::before {
	color: #66645d !important;
	font-weight: 500 !important;
}
.activeSign {
	margin: 24rpx 40rpx;
	border-radius: 12rpx;
	.title {
		line-height: 45rpx;
		color: #66645d;
		font-size: 32rpx;
		padding-bottom: 23rpx;
		background: #f7f7f7;
	}
	.active-changci {
		background-color: #ffffff;
		border-radius: 12rpx;
		overflow: hidden;
	}
	.massClass {
		padding: 23rpx 20rpx;
		font-size: 30rpx;
		font-weight: 400;
		color: #66645d;
		line-height: 42rpx;
		.tip {
			font-size: 24rpx;
			color: #f39800;
			margin-left: 48rpx;
		}
		position: relative;
		&::after {
			content: '';
			position: absolute;
			left: 20rpx;
			right: 20rpx;
			bottom: 0;
			height: 2rpx;
			background-color: rgba(102, 100, 93, 0.1);
		}
	}
	.active-list-item {
		padding: 0 24rpx;
		background: #fff;
	}
}
.chat {
	width: 670rpx;
	background: #fff;
	border-radius: 12rpx;
	min-height: 360rpx;
	position: relative;
	overflow: hidden;
	.input {
		padding: 40rpx 86rpx 60rpx 40rpx;
		background: #f7f7f7;

		.item {
			display: flex;
			align-items: center;
			// margin-bottom: 44rpx;
			padding: 20rpx 0 12rpx 0;
			.label {
				width: 120rpx;
				font-size: 30rpx;
				font-weight: bold;
				color: #66645d;
				line-height: 42rpx;
				// margin-right: 24rpx;
				display: flex;
				justify-content: space-between;
			}
			.uni-input {
				width: 400rpx;
				height: 60rpx;
				background: #f7f7f7;
				border-radius: 12rpx;
				margin: 0;
				font-size: 26rpx;
				padding-left: 12rpx;
				margin-left: 24rpx;
			}
			&:last-child {
				margin-bottom: 0;
			}
		}
	}
}
.checkList {
	display: flex;
	align-items: center;
	font-size: 28rpx;
	color: rgba(102, 100, 93, 0.5);
	border-bottom: 1px solid rgba(102, 100, 93, 0.1);
	padding: 24rpx 0;
}
.checkList:last-child {
	border-bottom: 1px solid #fff;
}
.time {
	margin-left: 12rpx;
	line-height: 40rpx;
}

.title {
	line-height: 100rpx;
	font-size: 36rpx;
}
.c-title {
	line-height: 100rpx;
	font-size: 26rpx;
}
.activityimg {
	width: 100%;
	margin-top: 10rpx;
}

.attention {
	display: flex;
	justify-content: flex-end;
	padding: 32rpx 20rpx 32rpx;
	color: rgba(178, 177, 174, 1);
	align-items: center;
	cursor: pointer;
}

.attentionText {
	margin-left: 14rpx;
	font-size: 24rpx;
}

.infoButton {
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-size: 34px;
	font-weight: 400;
	color: rgba(102, 100, 93, 1);
	background: rgba(250, 204, 107, 1);
	border-radius: 12rpx;
}

.infoButton navigator {
	flex: 1;
}

.infoButton button {
	flex: 1;
	background: rgba(250, 204, 107, 1);
	border-radius: 12rpx;
}

.infoButton button::after {
	border: 0;
	color: rgba(102, 100, 93, 1);
}

.commentTitle {
	height: 94rpx;
	line-height: 94rpx;
	display: flex;
	justify-content: space-between;
	font-size: 28rpx;
	font-weight: 500;
	align-items: center;
	border-bottom: 1px solid rgba(102, 100, 93, 0.1);
	padding: 0 40rpx;
}

.commentTitle h3 {
	font-size: 32rpx;
}

.commentBox {
	padding: 0 40rpx 40rpx;
}

.infoButtonLine {
	font-weight: lighter;
	font-size: 48rpx;
	color: rgba(102, 100, 93, 0.5);
}

.commentLi {
	background: rgba(255, 255, 255, 1);
	border-radius: 12rpx;
	margin-top: 24rpx;
	padding: 20rpx;
}

.commentFirst {
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.commentFirst image {
	width: 100rpx;
	height: 100rpx;
	border-radius: 50px;
}

.commentFirst h3 {
	flex: 1;
	font-size: 30rpx;
	font-weight: 400;
	color: rgba(102, 100, 93, 1);
	padding-left: 24rpx;
	line-height: 46rpx;
}

.commentIcon {
	font-size: 24rpx;
	font-weight: 400;
	color: rgba(178, 177, 174, 1);
	display: flex;
	justify-content: flex-start;
	margin-left: 20rpx;
}

.commentIcon image {
	width: 30rpx;
	height: 30rpx;
	margin-right: 12rpx;
}

.commentSecond {
	font-size: 28rpx;
	font-weight: 300;
	color: rgba(138, 140, 141, 1);
	padding-left: 14rpx;
	margin: 36rpx 0;
	line-height: 46rpx;
}

.commentThired {
	border-left: 1px solid rgba(138, 140, 141, 1);
	margin-bottom: 23rpx;
}

.commentThiredInfo {
	margin-top: 12rpx;
	line-height: 46rpx;
	padding-left: 20rpx;
	color: rgba(178, 177, 174, 1);
	font-size: 28rpx;
}

.infoTop {
	padding: 14rpx 23rpx;
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background-color: #ffffff;
}
.infoButtonClick {
	height: 82rpx;
	width: 100%;
	background: rgba(250, 204, 107, 1);
	line-height: 82rpx;
	text-align: center;
	font-size: 34rpx;
	color: #66645d;
}
.list-item-time {
	display: flex;
	align-items: flex-start;
	margin-bottom: 12rpx;
	line-height: 40rpx;

	.imageClass {
		width: 30rpx;
		height: 30rpx;
		flex-shrink: 0;
		margin-top: 5rpx;
	}
	.time-label {
		color: #66645d;
		line-height: 40rpx;
		flex-shrink: 0;
		margin-left: 12rpx;
	}
	.time {
		line-height: 40rpx;
	}
	&:last-child {
		margin-bottom: 0;
	}
}
.check {
	margin-right: 26rpx;
}
/deep/.uni-radio-input {
	margin: 0 !important;
}
.baseBtn {
	border-radius: 0 0 12rpx 12rpx;
	background-color: #f1f1f1;
	position: absolute;
	bottom: 0rpx;
	left: 0rpx;
	width: 100%;
}

/deep/input {
	border: 2rpx solid rgba(151, 151, 151, 0.5) !important;
}

.hidden {
	visibility: hidden;
	position: relative;
	z-index: -1;
}
.infoButtonClick[disabled] {
	background-color: #f1f1f1 !important;
	color: #ccc !important;
}
</style>
